package com.lwj.liwanjia.view.activity.work.shang_jia;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.PinLunHomeAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.HuifuBean;
import com.lwj.liwanjia.bean.PinLunHomeBean;
import com.lwj.liwanjia.bean.WorkBean08;
import com.lwj.liwanjia.bean.WorkBean09;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.impl.HuifuView;
import com.lwj.liwanjia.presenter.SendHuifuPresenter;
import com.lwj.liwanjia.utils.EditHuifuDialog;
import com.lwj.liwanjia.utils.PayCancleDialog;
import com.lwj.liwanjia.utils.SoftKeyBoardHight;
import com.lwj.liwanjia.utils.SpfUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/13.
 */

public class PinLunHomeActivity extends BaseActivity implements View.OnClickListener, HuifuView {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(PinLunHomeActivity.this, "数据有误", Toast.LENGTH_LONG).show();
                    break;

                case 1:

                    if (!TextUtils.isEmpty(quanBu)) {
                        tv_quanBu.setText(quanBu);
                    }

                    if (!TextUtils.isEmpty(hao)) {
                        tv_haoPin.setText(hao);
                    }

                    if (!TextUtils.isEmpty(zhong)) {
                        tv_zhongPin.setText(zhong);
                    }

                    if (!TextUtils.isEmpty(cha)) {
                        tv_chaPin.setText(cha);
                    }

                    if (!TextUtils.isEmpty(youTu)) {
                        tv_youTu.setText(youTu);
                    }

                    if (!TextUtils.isEmpty(zhui)) {
                        tv_zhuiJia.setText(zhui);
                    }

                    break;

                case 2:

                    if (homeList != null && homeList.size() > 0) {
                        mListView.setVisibility(View.VISIBLE);
                        tvRecord.setVisibility(View.GONE);
                        homeAdapter = new PinLunHomeAdapter(PinLunHomeActivity.this, homeList);
                        mListView.setAdapter(homeAdapter);
                        homeAdapter.setListener(new PinLunHomeAdapter.HuifuListener() {
                            @Override
                            public void huifu(View view, final int position) {
                                clickPosition = position;
                                d = new EditHuifuDialog(PinLunHomeActivity.this);
                                d.show();
                                d.setSendPinlun(new EditHuifuDialog.SendHuifu() {
                                    @Override
                                    public void send(String huiFu) {
                                        map.put("token", token);
                                        map.put("commentId", homeList.get(position).getCommentId());
                                        map.put("commentInfo", huiFu);
                                        presenter.sendHuifu(map);
                                    }
                                });
                            }
                        });
                    } else {
                        mListView.setVisibility(View.GONE);
                        tvRecord.setVisibility(View.VISIBLE);
                    }

                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private ListView mListView;
    private PinLunHomeAdapter homeAdapter;
    private List<WorkBean09.DataBean> homeList;
    private TextView tv_quanBu;//全部
    private TextView tv_haoPin;//好评
    private TextView tv_zhongPin;//中评
    private TextView tv_chaPin;//差评
    private TextView tv_youTu;//有图
    private TextView tv_zhuiJia;//追加
    private String quanBu;
    private String hao;
    private String zhong;
    private String cha;
    private String youTu;
    private String zhui;
    private SendHuifuPresenter presenter;
    private String token;
    private Map<String, Object> map = new HashMap<>();
    private EditHuifuDialog d;
    private String type = "全部";
    private int clickPosition;
    private TextView tvRecord;

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        mListView = (ListView) findViewById(R.id.listView);
        tv_quanBu = (TextView) findViewById(R.id.tv_quanBu);
        tv_haoPin = (TextView) findViewById(R.id.tv_haoPin);
        tv_zhongPin = (TextView) findViewById(R.id.tv_zhongPin);
        tv_chaPin = (TextView) findViewById(R.id.tv_chaPin);
        tv_youTu = (TextView) findViewById(R.id.tv_youTu);
        tv_zhuiJia = (TextView) findViewById(R.id.tv_zhuiJia);
        tvRecord = (TextView) findViewById(R.id.tv_record);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("评论查看");

        tv_back.setOnClickListener(this);
        tv_quanBu.setOnClickListener(this);
        tv_haoPin.setOnClickListener(this);
        tv_zhongPin.setOnClickListener(this);
        tv_chaPin.setOnClickListener(this);
        tv_youTu.setOnClickListener(this);
        tv_zhuiJia.setOnClickListener(this);
        presenter = new SendHuifuPresenter(this);
        token = SpfUtils.getSpfUtils(this).getToken();

    }

    @Override
    protected void initData() {

        //获取评论条数
        initOkhttp01();

        //获取数据
        initOkhttp02("全部");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ping_lun_home;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
            //全部
            case R.id.tv_quanBu:

                //更新UI
                initUi(tv_quanBu, tv_haoPin, tv_zhongPin, tv_chaPin, tv_youTu, tv_zhuiJia);

                initOkhttp02("全部");
                type = "全部";
                break;
            //好评
            case R.id.tv_haoPin:

                //更新UI
                initUi(tv_haoPin, tv_zhongPin, tv_chaPin, tv_youTu, tv_zhuiJia, tv_quanBu);

                initOkhttp02("好评");
                type = "好评";
                break;
            //中评
            case R.id.tv_zhongPin:

                //更新UI
                initUi(tv_zhongPin, tv_quanBu, tv_haoPin, tv_chaPin, tv_youTu, tv_zhuiJia);

                initOkhttp02("中评");
                type = "中评";
                break;
            //差评
            case R.id.tv_chaPin:

                //更新UI
                initUi(tv_chaPin, tv_quanBu, tv_haoPin, tv_zhongPin, tv_youTu, tv_zhuiJia);

                initOkhttp02("差评");
                type = "差评";
                break;
            //有图
            case R.id.tv_youTu:

                //更新UI
                initUi(tv_youTu, tv_quanBu, tv_haoPin, tv_zhongPin, tv_chaPin, tv_zhuiJia);

                initOkhttp02("有图");
                type = "有图";
                break;
            //追加
            case R.id.tv_zhuiJia:

                //更新UI
                initUi(tv_zhuiJia, tv_quanBu, tv_haoPin, tv_zhongPin, tv_chaPin, tv_youTu);

                initOkhttp02("追加");
                type = "追加";
                break;
        }
    }

    //更新UI
    private void initUi(TextView tv_press, TextView tv_02, TextView tv_03, TextView tv_04, TextView tv_05, TextView tv_06) {

        tv_press.setTextColor(Color.WHITE);
        tv_press.setBackgroundResource(R.drawable.shape_logo);
        tv_02.setTextColor(Color.parseColor("#EC921A"));
        tv_02.setBackgroundResource(R.drawable.shape_cheng_se);
        tv_03.setTextColor(Color.parseColor("#EC921A"));
        tv_03.setBackgroundResource(R.drawable.shape_cheng_se);
        tv_04.setTextColor(Color.parseColor("#EC921A"));
        tv_04.setBackgroundResource(R.drawable.shape_cheng_se);
        tv_05.setTextColor(Color.parseColor("#EC921A"));
        tv_05.setBackgroundResource(R.drawable.shape_cheng_se);
        tv_06.setTextColor(Color.parseColor("#EC921A"));
        tv_06.setBackgroundResource(R.drawable.shape_cheng_se);
    }

    //获取评论条数
    private void initOkhttp01() {

        String url = URLConstant.BASE_URL + "comment/loadCommentLabe.do";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "分类数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "分类图数据获取成功");

                String result = null;

                try {
                    result = response.body().string();
                    //Log.d("Tag",result);
                } catch (Exception e) {
                }

                parse01(result);
            }
        });
    }

    //解析评论条数
    private void parse01(String result) {

        Gson gson = new Gson();
        WorkBean08 bean = gson.fromJson(result, WorkBean08.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    quanBu = bean.getData().getAll();
                    hao = bean.getData().getGood();
                    zhong = bean.getData().getMiddle();
                    cha = bean.getData().getBad();
                    youTu = bean.getData().getHaveImg();
                    zhui = bean.getData().getAdd();

                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }

    //获取数据
    private void initOkhttp02(String type) {

        String url = URLConstant.BASE_URL + "comment/loadShopUserComment.do";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .add("type", type)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "分类数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "分类图数据获取成功");

                String result = null;

                try {
                    result = response.body().string();
                    //Log.d("Tag",result);
                } catch (Exception e) {
                }

                parse02(result);
            }
        });

    }

    //解析
    private void parse02(String result) {

        Gson gson = new Gson();
        WorkBean09 bean = gson.fromJson(result, WorkBean09.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    homeList = new ArrayList<>();

                    for (int i = 0; i < bean.getData().size(); i++) {

                        WorkBean09.DataBean dataBean = bean.getData().get(i);
                        homeList.add(dataBean);
                    }

                    mHandler.sendEmptyMessage(2);

                    break;
            }
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void sendHuifu(HuifuBean huifuBean) {
        if (huifuBean.getState() == 0) {
            Toast.makeText(this, "接口有误", Toast.LENGTH_SHORT).show();
        } else if (huifuBean.getState() == 1) {
            if (huifuBean.isData()) {
                Toast.makeText(this, "回复成功", Toast.LENGTH_SHORT).show();
                d.dismiss();
                initOkhttp02(type);
                mListView.smoothScrollToPosition(clickPosition);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter = null;
        }
    }
}
